package com.xxx.meter.ui.info;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.xxx.meter.R;
import com.xxx.meter.base.BaseActivity;
import com.xxx.meter.model.http.Api;
import com.xxx.meter.model.http.ApiCallback;
import com.xxx.meter.model.http.bean.PatrolRecordDetailBean;
import com.xxx.meter.model.http.bean.WorkInfoBean;
import com.xxx.meter.ui.info.adapter.InfoPatrolFindAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class PatrolRecordDetailActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.main_title)
    TextView mTitle;
    @BindView(R.id.main_recycler)
    RecyclerView mRecycler;
    @BindView(R.id.main_refresh)
    SwipeRefreshLayout mRefresh;
    @BindView(R.id.not_data)
    ImageView mNotData;
    @BindView(R.id.not_net)
    ImageView mNotNet;

    private List<PatrolRecordDetailBean> mList = new ArrayList<>();
    private InfoPatrolFindAdapter mAdapter;
    private int recordId;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_patrol_record_detail;
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        String name = intent.getStringExtra("name");
        recordId = intent.getIntExtra("recordId",0);
        mTitle.setText(name);

        mAdapter = new InfoPatrolFindAdapter(mList);
        mRecycler.setLayoutManager(new LinearLayoutManager(this));
        mRecycler.setAdapter(mAdapter);
        mRefresh.setOnRefreshListener(this);

        loadInfo(true);
    }

    @OnClick({R.id.main_return, R.id.not_data, R.id.not_net})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.main_return:
                finish();
                break;
            case R.id.not_data:
                onRefresh();
                break;
            case R.id.not_net:
                onRefresh();
                break;
        }
    }

    @Override
    public void onRefresh() {
        loadInfo(false);
    }

    /**
     * @param isRefresh 是否刷新
     * @Model 获取机器巡查数据
     */
    private void loadInfo(boolean isRefresh) {
        Api.getInstance().getPatrolRecordDetail(recordId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ApiCallback<List<PatrolRecordDetailBean>>(this, isRefresh) {
                    @Override
                    public void onSuccess(List<PatrolRecordDetailBean> list) {
                        if (list == null || list.size() == 0) {
                            mRefresh.setVisibility(View.GONE);
                            mNotData.setVisibility(View.VISIBLE);
                            mNotNet.setVisibility(View.GONE);
                        } else {
                            mRefresh.setVisibility(View.VISIBLE);
                            mNotData.setVisibility(View.GONE);
                            mNotNet.setVisibility(View.GONE);
                            mList.clear();
                            mList.addAll(list);
                            mAdapter.notifyDataSetChanged();
                        }
                        mRefresh.setRefreshing(false);
                    }

                    @Override
                    public void onError(int errorCode, String errorMessage) {
                        mRefresh.setVisibility(View.GONE);
                        mNotData.setVisibility(View.GONE);
                        mNotNet.setVisibility(View.VISIBLE);
                        mRefresh.setRefreshing(false);
                        mAdapter.loadMoreEnd(true);
                    }
                });
    }

}
